
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>基础类型 · Golang 学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="韩亚军">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-image-captions/image-captions.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism-okaidia.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-disqus/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    

    
        <link rel="shortcut icon" href='images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='images/favicon.ico' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='images/favicon.ico'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="images/favicon.ico">
        
        <link rel="apple-touch-icon" sizes="180x180" href="images/favicon.ico">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"repo":"Han-Ya-Jun/golang-notes","types":["star"],"size":"small"};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://hanyajun.com" target="_blank" class="custom-link">韩亚军的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                        <b>1.1.</b>
                    
                    序言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="INTRODUCE.html">
            
                <a href="INTRODUCE.html">
            
                    
                        <b>1.2.</b>
                    
                    go语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                        <b>1.3.</b>
                    
                    基础语法
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.1" data-path="BASICGRAMMAR.html">
            
                <a href="BASICGRAMMAR.html">
            
                    
                        <b>1.3.1.</b>
                    
                    基础类型
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                        <b>1.4.</b>
                    
                    Go每日新闻
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="gocn_news_2017.html">
            
                <a href="gocn_news_2017.html">
            
                    
                        <b>1.4.1.</b>
                    
                    GoCN_NEWS_2017
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="gocn_news_2018.html">
            
                <a href="gocn_news_2018.html">
            
                    
                        <b>1.4.2.</b>
                    
                    GoCN_NEWS_2018
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="gocn_news_2019.html">
            
                <a href="gocn_news_2019.html">
            
                    
                        <b>1.4.3.</b>
                    
                    GoCN_NEWS_2019
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >基础类型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6570;&#x636E;&#x7C7B;&#x578B;">&#x6570;&#x636E;&#x7C7B;&#x578B;</h1>
<h2 id="&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;">&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
<ul>
<li><p>&#x6574;&#x6570;&#x7C7B;&#x578B;&#xFF1A;</p>
<ul>
<li><p>&#x7CFB;&#x7EDF;&#x67B6;&#x6784;&#x6709;&#x5173;&#x7684;&#xFF1A;int&#x4E0E;uint&#xFF08;&#x5728;32&#x4F4D;&#x7CFB;&#x7EDF;&#x4E0B;&#x4E3A;32&#xFF0C;64&#x4F4D;&#x7CFB;&#x7EDF;&#x4E0B;&#x4E3A;64&#xFF09;&#xFF1B;</p>
</li>
<li><p>&#x56FA;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#xFF1A; int8/16/32/64&#x3001;uint8/16/32/64&#xFF1B;</p>
</li>
<li><p>&#x6574;&#x6570;&#x7C7B;&#x578B;&#x8BE6;&#x7EC6;&#x8868;
<img src="http://cdn.hanyajun.com/20190630_163623_int.png" alt=""></p>
</li>
<li><p>&#x6574;&#x6570;&#x7C7B;&#x578B;&#x4F8B;&#x5B50;</p>
<pre class="language-"><code class="lang-go"><span class="token comment">//1&#x3001;&#x7C7B;&#x578B;&#x8868;&#x793A;</span>
<span class="token comment">//int&#x548C;int32&#x4E3A;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#xFF0C;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x9700;&#x8981;&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</span>
<span class="token comment">//&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x9700;&#x6CE8;&#x610F;&#x7CBE;&#x5EA6;&#x635F;&#x5931;&#xFF08;&#x6D6E;&#x70B9;&#x6570;&#x2192;&#x6574;&#x6570;&#xFF09;&#xFF0C;&#x503C;&#x6EA2;&#x51FA;&#xFF08;&#x5927;&#x8303;&#x56F4;&#x2192;&#x5C0F;&#x8303;&#x56F4;&#xFF09;</span>
<span class="token keyword">var</span> v2 <span class="token builtin">int32</span>
v1<span class="token operator">:=</span><span class="token number">64</span>
v2<span class="token operator">=</span><span class="token function">int32</span><span class="token punctuation">(</span>v1<span class="token punctuation">)</span>
<span class="token comment">//2&#x3001;&#x6570;&#x503C;&#x8FD0;&#x7B97;,&#x652F;&#x6301;&#x201C;+,-,*,/&#x548C;%&#x201D;</span>
<span class="token number">5</span><span class="token operator">%</span><span class="token number">3</span> <span class="token comment">//&#x6C42;&#x4F59;</span>
<span class="token comment">//3&#x3001;&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;,&#x201C;&lt;,&gt;,==,&gt;=,&lt;=,!=&#x201D;</span>
<span class="token comment">//&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x4F8B;&#x5982;int&#x548C;int8&#xFF0C;&#x4F46;&#x53EF;&#x4EE5;&#x4E0E;&#x5B57;&#x9762;&#x5E38;&#x91CF;&#xFF08;literal&#xFF09;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;</span>
<span class="token keyword">var</span> i <span class="token builtin">int32</span>
<span class="token keyword">var</span> j <span class="token builtin">int64</span>
i<span class="token punctuation">,</span>j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span>
<span class="token keyword">if</span> i<span class="token operator">==</span>j  <span class="token comment">//&#x7F16;&#x8BD1;&#x9519;&#x8BEF;&#xFF0C;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x4E0D;&#x80FD;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;</span>
<span class="token keyword">if</span> i<span class="token operator">==</span><span class="token number">1</span> <span class="token operator">||</span> j<span class="token operator">==</span><span class="token number">2</span>  <span class="token comment">//&#x7F16;&#x8BD1;&#x901A;&#x8FC7;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0E;&#x5B57;&#x9762;&#x5E38;&#x91CF;&#xFF08;literal&#xFF09;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;</span>
<span class="token comment">//4&#x3001;&#x4F4D;&#x8FD0;&#x7B97;</span>
<span class="token comment">//Go(^x)&#x53D6;&#x53CD;&#x4E0E;C&#x8BED;&#x8A00;(~x)&#x4E0D;&#x540C;&#xFF0C;&#x5176;&#x4ED6;&#x7C7B;&#x4F3C;&#xFF0C;&#x5177;&#x4F53;&#x89C1;&#x4E0B;&#x8868;</span>
</code></pre>
</li>
<li><p>&#x6574;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x4F4D;&#x8FD0;&#x7B97;
<img src="http://cdn.hanyajun.com/20190630_164116_bit-operation.png" alt=""></p>
</li>
</ul>
</li>
<li><p>&#x6D6E;&#x70B9;&#x7C7B;&#x578B;&#xFF1A;float32&#x3001;float64</p>
<pre class="language-"><code class="lang-go"><span class="token comment">//1&#x3001;&#x6D6E;&#x70B9;&#x578B;&#x5206;&#x4E3A;float32(&#x7C7B;&#x4F3C;C&#x4E2D;&#x7684;float)&#xFF0C;float64(&#x7C7B;&#x4F3C;C&#x4E2D;&#x7684;double)</span>
<span class="token keyword">var</span> f1 <span class="token builtin">float32</span>
f1<span class="token operator">=</span><span class="token number">12</span>     <span class="token comment">//&#x4E0D;&#x52A0;&#x5C0F;&#x6570;&#x70B9;&#xFF0C;&#x88AB;&#x63A8;&#x5BFC;&#x4E3A;&#x6574;&#x578B;</span>
f2<span class="token operator">:=</span><span class="token number">12.0</span>  <span class="token comment">//&#x52A0;&#x5C0F;&#x6570;&#x70B9;&#xFF0C;&#x88AB;&#x63A8;&#x5BFC;&#x4E3A;float64</span>
f1<span class="token operator">=</span><span class="token function">float32</span><span class="token punctuation">(</span>f2<span class="token punctuation">)</span>  <span class="token comment">//&#x9700;&#x8981;&#x6267;&#x884C;&#x5F3A;&#x5236;&#x8F6C;&#x6362;</span>
<span class="token comment">//2&#x3001;&#x6D6E;&#x70B9;&#x6570;&#x7684;&#x6BD4;&#x8F83;</span>
<span class="token comment">//&#x6D6E;&#x70B9;&#x6570;&#x4E0D;&#x662F;&#x7CBE;&#x786E;&#x7684;&#x8868;&#x8FBE;&#x65B9;&#x5F0F;&#xFF0C;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x201C;==&#x201D;&#x6765;&#x5224;&#x65AD;&#x662F;&#x5426;&#x76F8;&#x7B49;&#xFF0C;&#x53EF;&#x4EE5;&#x501F;&#x7528;math&#x7684;&#x5305;math.Fdim</span>
</code></pre>
</li>
</ul>
<ul>
<li><p>&#x5E03;&#x5C14;&#x7C7B;&#x578B;&#xFF1A;bool</p>
<pre class="language-"><code class="lang-go"><span class="token comment">//&#x5E03;&#x5C14;&#x7C7B;&#x578B;&#x7684;&#x5173;&#x952E;&#x5B57;&#x4E3A;bool,&#x503C;&#x4E3A;true&#x6216;false&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;false</span>
<span class="token keyword">var</span> v1 <span class="token builtin">bool</span>
v1<span class="token operator">=</span><span class="token boolean">true</span>
<span class="token comment">//&#x63A5;&#x53D7;&#x8868;&#x8FBE;&#x5F0F;&#x5224;&#x65AD;&#x8D4B;&#x503C;&#xFF0C;&#x4E0D;&#x652F;&#x6301;&#x81EA;&#x52A8;&#x6216;&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</span>
v2<span class="token operator">:=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">==</span><span class="token number">2</span><span class="token punctuation">)</span>
</code></pre>
</li>
<li><p>&#x590D;&#x6570;&#x7C7B;&#x578B;&#xFF1A; complex64&#x3001;complex128</p>
<pre class="language-"><code class="lang-go"><span class="token comment">//1&#x3001;&#x590D;&#x6570;&#x7684;&#x8868;&#x793A;</span>
<span class="token keyword">var</span> v1 <span class="token builtin">complex64</span>
v1<span class="token operator">=</span><span class="token number">3.2</span><span class="token operator">+</span><span class="token number">12i</span>
<span class="token comment">//v1 v2 v3 &#x8868;&#x793A;&#x4E3A;&#x540C;&#x4E00;&#x4E2A;&#x6570;</span>
v2<span class="token operator">:=</span><span class="token number">3.2</span><span class="token operator">+</span><span class="token number">12i</span>
v3<span class="token operator">:=</span><span class="token function">complex</span><span class="token punctuation">(</span><span class="token number">3.2</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">)</span>
<span class="token comment">//2&#x3001;&#x5B9E;&#x90E8;&#x4E0E;&#x865A;&#x90E8;</span>
<span class="token comment">//z=complex(x,y),&#x901A;&#x8FC7;&#x5185;&#x7F6E;&#x51FD;&#x6570;&#x5B9E;&#x90E8;x=real(z),&#x865A;&#x90E8;y=imag(z)</span>
</code></pre>
</li>
</ul>
<ul>
<li><p>&#x5B57;&#x7B26;&#x7C7B;&#x578B;&#xFF1A; byte&#xFF08;uint8&#x7684;&#x522B;&#x540D;&#xFF09;&#x3001;rune&#xFF08;int32&#x7684;&#x522B;&#x540D;&#xFF0C;&#x6807;&#x8BC6;Unicode&#xFF09;</p>
</li>
<li><p>&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;&#xFF1A; string</p>
<ul>
<li>&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x4E00;&#x4E2A;&#x4E0D;&#x53EF;&#x6539;&#x53D8;&#x7684;&#x5B57;&#x8282;&#x5E8F;&#x5217;&#xFF0C;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4EFB;&#x610F;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5305;&#x62EC;byte&#x503C;0&#xFF1B;</li>
</ul>
</li>
<li><p>for range &#x8BED;&#x6CD5;&#x5BF9;UTF8&#x5B57;&#x7B26;&#x4E32;&#x63D0;&#x4F9B;&#x4E86;&#x7279;&#x6B8A;&#x652F;&#x6301;&#xFF1B;</p>
<ul>
<li>&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x548C; []rune &#x7C7B;&#x578B;&#x7684;&#x76F8;&#x4E92;&#x8F6C;&#x6362;&#x63D0;&#x4F9B;&#x4E86;&#x652F;&#x6301;&#xFF1A;&#x5982;utf8.RuneCountInString(str)&#xFF1B;</li>
</ul>
</li>
<li><p>&#x5B57;&#x7B26;&#x4E32;&#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x56E0;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8D4B;&#x503C;&#x64CD;&#x4F5C;&#x4E5F;&#x5C31;&#x662F;reflect.StringHeader &#x7ED3;&#x6784;&#x4F53;&#x7684;&#x590D;&#x5236;&#x8FC7;&#x7A0B;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x6D89;&#x53CA;&#x5E95;&#x5C42;&#x5B57;&#x8282;&#x6570;&#x7EC4;&#x7684;&#x590D;&#x5236;&#x3002;</p>
<pre class="language-"><code class="lang-go"><span class="token comment">//&#x58F0;&#x660E;&#x4E0E;&#x8D4B;&#x503C;</span>
<span class="token keyword">var</span> str <span class="token builtin">string</span>
str<span class="token operator">=</span><span class="token string">&quot;hello world&quot;</span>
</code></pre>
</li>
</ul>
<h2 id="&#x9AD8;&#x7EA7;&#x6570;&#x636E;&#x7C7B;&#x578B;">&#x9AD8;&#x7EA7;&#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
<ul>
<li><p>&#x6570;&#x7EC4;&#xFF1A;[N]T&#xFF0C;[...]T</p>
</li>
<li><p>&#x5207;&#x7247;&#xFF1A;[]T</p>
</li>
<li><p>&#x5B57;&#x5178;&#xFF1A;map[K]T</p>
</li>
<li><p>&#x901A;&#x9053;&#x7C7B;&#x578B;&#xFF1A;chan T</p>
<ul>
<li><p>&#x5411;&#x65E0;&#x7F13;&#x5B58;&#x901A;&#x9053;&#x5199;&#x5165;&#x65F6;&#xFF0C;&#x4F1A;&#x963B;&#x585E;&#x7B49;&#x5F85;&#x8BFB;&#x53D6;&#xFF1B;</p>
</li>
<li><p>&#x5173;&#x95ED;nil&#x6216;&#x5DF2;&#x5173;&#x95ED;&#x901A;&#x9053;&#x4F1A;&#x5F15;&#x53D1;panic&#xFF1B;</p>
</li>
<li><p>&#x5411;&#x5DF2;&#x5173;&#x95ED;&#x7684;&#x901A;&#x9053;&#x53D1;&#x751F;&#x6570;&#x636E;&#x4F1A;&#x5F15;&#x53D1;panic&#xFF1B;</p>
</li>
<li><p>&#x4ECE;&#x5DF2;&#x5173;&#x95ED;&#x7684;&#x901A;&#x9053;&#x8BFB;&#x53D6;&#xFF0C;&#x603B;&#x662F;&#x8FD4;&#x56DE;0&#x503C;&#x548C;false&#xFF08;&#x5BF9;&#x4E8E;&#x7F13;&#x5B58;&#x901A;&#x9053;&#xFF0C;&#x5148;&#x8BFB;&#x53D6;&#x7F13;&#x5B58;&#x7684;&#x6570;&#x636E;&#xFF09;&#xFF1B;</p>
</li>
</ul>
<h2 id="&#x81EA;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x7C7B;&#x578B;">&#x81EA;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
</li>
<li><p>&#x81EA;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF08;&#x53EF;&#x4F7F;&#x7528;type&#x5B9A;&#x4E49;&#xFF09;&#xFF1A;</p>
</li>
<li><p>&#x522B;&#x540D;&#xFF1A;type BuffSize int&#xFF1B;BuffSize&#x4E3A;&#x65B0;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5E95;&#x5C42;&#x7C7B;&#x578B;&#x4E0E;int&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x662F;&#x4E3A;&#x4E0D;&#x540C;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E92;&#x64CD;&#x4F5C;&#x65F6;&#x8981;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;(BuffSize(var))&#xFF1B;</p>
</li>
<li><p>&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#xFF1A; type Name struct { ... }</p>
</li>
</ul>
<h2 id="&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x51E0;&#x70B9;">&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x51E0;&#x70B9;</h2>
<pre class="language-"><code class="lang-go"><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;fmt&quot;</span>
    <span class="token string">&quot;math&quot;</span>
    <span class="token string">&quot;math/cmplx&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
    <span class="token comment">//1.&#x5916;&#x90E8;&#x7684;&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x5FC5;&#x987B;&#x4F7F;&#x7528;var</span>
    aa <span class="token operator">=</span> <span class="token number">3</span>
    ss <span class="token operator">=</span> <span class="token string">&quot;kkk&quot;</span>
    bb <span class="token operator">=</span> <span class="token boolean">true</span>
<span class="token punctuation">)</span>

    <span class="token comment">// 2.&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x521D;&#x59CB;&#x503C;</span>
<span class="token keyword">func</span> <span class="token function">variableZeroValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> a <span class="token builtin">int</span>
    <span class="token keyword">var</span> s <span class="token builtin">string</span>
    <span class="token keyword">var</span> c <span class="token builtin">bool</span>
    fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d %q\n %v&quot;</span><span class="token punctuation">,</span> a<span class="token punctuation">,</span> s<span class="token punctuation">,</span>c<span class="token punctuation">)</span><span class="token comment">//  0 &quot;&quot;  false</span>
<span class="token punctuation">}</span>
   <span class="token comment">// 3. &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;  var &#x53D8;&#x91CF;&#x540D; &#x7C7B;&#x578B; = &#x503C;   &#x6765;&#x521D;&#x59CB;&#x5316;&#x53D8;&#x91CF;&#xFF0C;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;,&#x6765;&#x5206;&#x5272;</span>
<span class="token keyword">func</span> <span class="token function">variableInitialValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> a<span class="token punctuation">,</span> b <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span>
    <span class="token keyword">var</span> s <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;abc&quot;</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> s<span class="token punctuation">)</span>  <span class="token comment">// 3 4 abc</span>
<span class="token punctuation">}</span>
  <span class="token comment">// 4. &#x76F8;&#x6BD4;&#x4E0A;&#x9762;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x53D8;&#x91CF;&#x7C7B;&#x578B;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x6839;&#x636E;&#x540E;&#x9762;&#x521D;&#x59CB;&#x5316;&#x7684;&#x503C;&#x6765;&#x81EA;&#x52A8;&#x786E;&#x5B9A;&#x7C7B;&#x578B;</span>
<span class="token keyword">func</span> <span class="token function">variableTypeDeduction</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> s <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;def&quot;</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> s<span class="token punctuation">)</span>  <span class="token comment">//3 4 true def</span>
<span class="token punctuation">}</span>
<span class="token comment">// 5. &#x4F7F;&#x7528; : &#x53EF;&#x4EE5;&#x4EE3;&#x66FF;var &#x7684;&#x4F5C;&#x7528;</span>
<span class="token keyword">func</span> <span class="token function">variableShorter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> s <span class="token operator">:=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;def&quot;</span>
    b <span class="token operator">=</span> <span class="token number">5</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> s<span class="token punctuation">)</span>  <span class="token comment">// 3 5 true def</span>
<span class="token punctuation">}</span>
<span class="token comment">// 6. &#x590D;&#x6570;&#x7684;&#x4F7F;&#x7528;</span>
<span class="token keyword">func</span> <span class="token function">euler</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%.3f\n&quot;</span><span class="token punctuation">,</span>
        cmplx<span class="token punctuation">.</span><span class="token function">Exp</span><span class="token punctuation">(</span><span class="token number">1i</span><span class="token operator">*</span>math<span class="token punctuation">.</span>Pi<span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">triangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> a<span class="token punctuation">,</span> b <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token function">calcTriangle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// 7. &#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x6362;</span>
<span class="token keyword">func</span> <span class="token function">calcTriangle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> c <span class="token builtin">int</span>
    c <span class="token operator">=</span> <span class="token function">int</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Sqrt</span><span class="token punctuation">(</span><span class="token function">float64</span><span class="token punctuation">(</span>a<span class="token operator">*</span>a <span class="token operator">+</span> b<span class="token operator">*</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> c
<span class="token punctuation">}</span>
<span class="token comment">// 8. &#x4E0D;&#x6307;&#x5B9A; a,b&#x7684;&#x7C7B;&#x578B;</span>
<span class="token keyword">func</span> <span class="token function">consts</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">(</span>
        filename <span class="token operator">=</span> <span class="token string">&quot;abc.txt&quot;</span>
        a<span class="token punctuation">,</span> b     <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span>
    <span class="token punctuation">)</span>
    <span class="token keyword">var</span> c <span class="token builtin">int</span>
    c <span class="token operator">=</span> <span class="token function">int</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span><span class="token function">Sqrt</span><span class="token punctuation">(</span>a<span class="token operator">*</span>a <span class="token operator">+</span> b<span class="token operator">*</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// &#x7CFB;&#x7EDF;&#x5224;&#x5B9A;a&#xFF0C;b&#x4E3A;float</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>filename<span class="token punctuation">,</span> c<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">enums</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">(</span>
        cpp <span class="token operator">=</span> <span class="token boolean">iota</span>  <span class="token comment">// &#x81EA;&#x589E;&#x5E38;&#x91CF;</span>
        <span class="token boolean">_</span>
        python
        golang
        javascript
    <span class="token punctuation">)</span>
    <span class="token comment">// &#x5E38;&#x91CF;&#x5B9A;&#x4E49;&#x4F7F;&#x7528;const&#x5B9A;&#x4E49;</span>
    <span class="token keyword">const</span> <span class="token punctuation">(</span>
        b <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">&lt;&lt;</span> <span class="token punctuation">(</span><span class="token number">10</span> <span class="token operator">*</span> <span class="token boolean">iota</span><span class="token punctuation">)</span>
        kb
        mb
        gb
        tb
        pb
    <span class="token punctuation">)</span>

    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>cpp<span class="token punctuation">,</span> javascript<span class="token punctuation">,</span> python<span class="token punctuation">,</span> golang<span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> kb<span class="token punctuation">,</span> mb<span class="token punctuation">,</span> gb<span class="token punctuation">,</span> tb<span class="token punctuation">,</span> pb<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello world&quot;</span><span class="token punctuation">)</span>
    <span class="token function">variableZeroValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">variableInitialValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">variableTypeDeduction</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">variableShorter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>aa<span class="token punctuation">,</span> ss<span class="token punctuation">,</span> bb<span class="token punctuation">)</span>
    <span class="token function">euler</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">triangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">consts</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">enums</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; hanyajun.com all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">Updated at 
2019-06-30 22:36:32
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"基础类型","level":"1.3.1","depth":2,"next":{"title":"Go每日新闻","level":"1.4","depth":1,"ref":"","articles":[{"title":"GoCN_NEWS_2017","level":"1.4.1","depth":2,"path":"gocn_news_2017.md","ref":"gocn_news_2017.md","articles":[]},{"title":"GoCN_NEWS_2018","level":"1.4.2","depth":2,"path":"gocn_news_2018.md","ref":"gocn_news_2018.md","articles":[]},{"title":"GoCN_NEWS_2019","level":"1.4.3","depth":2,"path":"gocn_news_2019.md","ref":"gocn_news_2019.md","articles":[]}]},"previous":{"title":"基础语法","level":"1.3","depth":1,"ref":"","articles":[{"title":"基础类型","level":"1.3.1","depth":2,"path":"BASICGRAMMAR.md","ref":"BASICGRAMMAR.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["github","codesnippet","splitter","page-toc-button","image-captions","editlink","back-to-top-button","-lunr","-search","search-plus","github-buttons@2.1.0","favicon@^0.0.2","tbfed-pagefooter@^0.0.1","3-ba","theme-default","-highlight","prism","prism-themes","sitemap-general","ga","disqus","donate","livereload"],"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright © hanyajun.com","modify_label":"Updated at ","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-okaidia.css"]},"disqus":{"useIdentifier":false,"shortName":"hanyajun"},"github":{"url":"https://github.com/Han-Ya-Jun"},"editlink":{"label":"编辑本页","multilingual":false,"base":"https://github.com/Han-Ya-Jun/golang-notes/blob/master/"},"livereload":{},"splitter":{},"donate":{"alipay":"http://cdn.hanyajun.com/alipay.jpg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://cdn.hanyajun.com/wepay.png","wechatText":"微信打赏"},"codesnippet":{},"sitemap-general":{"prefix":"https://hanyajun.com/golang-notes/"},"fontsettings":{"theme":"white","family":"sans","size":2},"favicon":{"shortcut":"images/favicon.ico","bookmark":"images/favicon.ico","appleTouch":"images/favicon.ico","appleTouchMore":{"120x120":"images/favicon.ico","180x180":"images/favicon.ico"}},"page-toc-button":{},"back-to-top-button":{},"prism-themes":{},"github-buttons":{"repo":"Han-Ya-Jun/golang-notes","types":["star"],"size":"small"},"3-ba":{"configuration":"auto","token":"a79d548adc1c9b6a3d8b8ba1e8d0620c"},"ga":{"configuration":"auto","token":"UA-138985430-1"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"showLevel":true,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"search-plus":{},"image-captions":{"caption":"图片 - _CAPTION_","variable_name":"_pictures"}},"theme":"default","author":"韩亚军","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[]},"title":"Golang 学习笔记","language":"zh-hans","links":{"sidebar":{"韩亚军的博客":"https://hanyajun.com"}},"gitbook":"*","description":"Golang 学习笔记"},"file":{"path":"BASICGRAMMAR.md","mtime":"2019-06-30T14:36:32.945Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-20T06:00:19.327Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-editlink/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-3-ba/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-ga/plugin.js"></script>
        
    
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.16.1/URI.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-disqus/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

